#include <rtthread.h>
#include "dio_bsp.h"
#include "kits/fifo.h"
#include "rtc_bsp.h"
#include "sys_conf.h"
#include "user_mb_app.h"

static void daq_gvar_update(void);

/**
 * @brief  get ai and di readings and update global status register periodically
 * @param  none
 * @retval none
 **/
void daq_thread_entry(void *parameter)
{
    rt_thread_delay(DAQ_THREAD_DELAY);
    drv_dio_bsp_init();
    dio_reg_init();
    drv_di_timer_init();
    rt_thread_delay(300);
    while (1) {
        di_reg_update();
        daq_gvar_update();
        rt_thread_delay(1000);
    }
}

/**
 * @brief  update global variable g_din_inst and g_ain_inst according to di and ai inputs
 * @param  none
 * @retval none
 **/
static void daq_gvar_update(void)
{
    extern sys_reg_st g_sys;
    di_sts_update(&g_sys);  // update g_din_inst
    // rt_kprintf("din_bitmap[0]:-----------%04x\r\n", g_sys.status.din_bitmap[0]);
    mbm_sts_update(&g_sys, MB_MASTER_0);  // update modbus master components status
    mbm_sts_update(&g_sys, MB_MASTER_1);  // update modbus master components status
    return;
}
